package com.vcms.controller.customer.states;

import com.vcms.controller.customer.ChangeGiver;
import com.vcms.controller.customer.CoinReceiver;
import com.vcms.controller.customer.DispenseController;
import com.vcms.controller.customer.TransactionController;
import com.vcms.controller.storemanagement.StoreController;
import com.vcms.entity.store.item.StoreItem;
import com.vcms.exception.VMCSException;

public class DispenseDrinkState extends TransactionState {

	public DispenseDrinkState(TransactionController transactionController){
		setTransactionController(transactionController);
	}
	
	@Override
	public boolean handleDisplayCustomerPanel() {
		System.out.println("[DispenseDrinkState] - Incorrect state to handle display customer panel!");
		
		return false;
	}
	
	@Override
	public boolean handleStartTransaction() {
		System.out.println("[DispenseDrinkState] - Incorrect state to handle start transaction!");
		
		return false;
	}
	
	@Override
	public boolean handleProcessMoneyReceived() {
		System.out.println("[DispenseDrinkState] - Incorrect state to handle process money received!");
		
		return false;
	}
	
	@Override
	public boolean handleCompleteTransaction() {
		System.out.println("Inside Dispense Drink State Handle - Complete Transaction");
		
		TransactionController transactionController = getTransactionController();
		DispenseController dispenseController = transactionController.getDispenseController();
		int selection = transactionController.getSelection();
		
		//Register observers
		StoreItem item;
		try {
			item = transactionController.getMainControler().getStoreController().getStoreItem(StoreController.DRINK, selection);
			item.attach(dispenseController);
			item.attach(transactionController.getMainController().getMachinaryController());
		} catch (VMCSException e) {
			e.printStackTrace();
		}
		
		try{
			boolean drinkDispensed = dispenseController.dispenseDrink(selection);
			transactionController.setDrinkDispense(drinkDispensed);
			if(!drinkDispensed){
				// Drinks must dispense, otherwise result in fault
/*				TerminateState terminateState = new TerminateState(transactionController);
				transactionController.transitState(terminateState);
				
				transactionController.terminateTransaction();*/
				
				return false;
			}
			
			int totalInserted = transactionController.getTotalInserted();
			float price = transactionController.getPrice();
			ChangeGiver changeGiver = transactionController.getChangeGiver();
			
			float changeRequired = totalInserted - price;
			if(changeRequired > 0){
				boolean changeGiven = changeGiver.giveChange(changeRequired);
				transactionController.setChangeGiven(changeGiven);
				
				if(!changeGiven){
					// Change must be given, otherwise result in fault
/*					TerminateState terminateState = new TerminateState(transactionController);
					transactionController.transitState(terminateState);
					
					transactionController.terminateTransaction();*/
					return false;
				}
			}
			
			CoinReceiver coinReceiver = transactionController.getCoinReceiver();
				
			boolean storeCash = coinReceiver.storeCash();
			
			if(!storeCash){
				// Coin must be stored, otherwise result in fault
/*				TerminateState terminateState = new TerminateState(transactionController);
				transactionController.transitState(terminateState);
				
				transactionController.terminateTransaction();*/
				return false;
			}
			
			dispenseController.allowSelection(true);	
			
			// Transit to Ready State
			transactionController.transitState(StateFactory.getInstance().create(TransactionState.READY, transactionController));
			
		}catch(VMCSException ex){
			//transactionController.terminateTransaction();
//			throw ex;
		}
		
		return true;
	}
	
	@Override
	public boolean handleTerminateTransaction() {
		System.out.println("[DispenseDrinkState] - Incorrect state to terminate transaction!");
		
		return false;
	}
	
	@Override
	public boolean handleTerminateFault() {
		System.out.println("[DispenseDrinkState] - Incorrect state to handle terminate fault!");
		
		return false;
	}
	
	@Override
	public boolean handleRefreshCustomerPanel() {
		System.out.println("[DispenseDrinkState] - Incorrect state to handle refresh customer panel!");
		
		return false;
	}

	public void printState(){
		System.out.println("- Dispense Drink State -");
	}
}
